package source;

import bean.Order;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;

import java.util.Random;
import java.util.UUID;


/**
 *
 *  * 每隔1秒随机生成一条订单信息(订单ID、用户ID、订单金额、时间戳)
 *  * 要求:
 *  * - 随机生成订单ID(UUID)
 *  * - 随机生成用户ID(0-2)
 *  * - 随机生成订单金额(0-100)
 *  * - 时间戳为当前系统时间
 *
 *
 * SourceFunction:非并行数据源(并行度只能=1)
 * RichSourceFunction:多功能非并行数据源(并行度只能=1)
 * ParallelSourceFunction:并行数据源(并行度能够>=1)
 * RichParallelSourceFunction:多功能并行数据源(并行度能够>=1)--后续学习的Kafka数据源使用的就是该接口
 */

public class MyOrderSource extends RichParallelSourceFunction<Order> {

    private Boolean flag = true;

    @Override
    public void run(SourceContext<Order> sourceContext) throws Exception {
        Random random = new Random();
        while (flag){
            Thread.sleep(1000);
            String id = UUID.randomUUID().toString();
            int userId = random.nextInt(3);
            int money = random.nextInt(101);
            long createTime = System.currentTimeMillis();
            sourceContext.collect(new Order(id,userId,money,createTime));
        }
    }

    @Override
    public void cancel() {
        flag = false;
    }
}
